package com.spark.check

import android.annotation.SuppressLint
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.spark.check.model.ChargePeopleModel
import com.spark.check.widget.ItemChargeTaskView
import com.spark.colliery.common.ext.gone
import com.spark.colliery.common.ext.isVisible
import com.spark.common.BaseFragment
import com.spark.common.Const
import com.spark.common.ext.server
import com.spark.common.ext.wrapIO
import com.spark.common.net.Http
import kotlinx.android.synthetic.main.frag_charge_people.*

/**
 * the people who charge this project
 */
class ChargePeopleFragment : BaseFragment() {
    override fun contentResId(): Int {
        return R.layout.frag_charge_people
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val chargePeopleAdapter = ChargePeopleAdapter(R.layout.item_charge_people)
        recy_resp.adapter = chargePeopleAdapter
        recy_resp.layoutManager = LinearLayoutManager(context)

        chargePeopleAdapter.addData(ChargePeopleModel(
                name = "李存勖",
                avatar = "R.drawable.img_default_avatar",
                tasks = mutableListOf(
                        ChargePeopleModel.Task(
                                serialno = "1.1.1",
                                chargepeople = "李崇贵",
                                content = "露天煤矿-岗位规范-专业技能及专业"
                        ),
                        ChargePeopleModel.Task(
                                serialno = "1.1.1",
                                chargepeople = "李崇贵",
                                content = "露天煤矿-岗位规范-专业技能及专业"
                        ),
                        ChargePeopleModel.Task(
                                serialno = "1.1.1",
                                chargepeople = "李崇贵",
                                content = "露天煤矿-岗位规范-专业技能及专业"
                        ),
                        ChargePeopleModel.Task(
                                serialno = "1.1.1",
                                chargepeople = "李崇贵",
                                content = "露天煤矿-岗位规范-专业技能及专业\n露天煤矿-岗位规范-专业技能及专业\n露天煤矿-岗位规范-专业技能及专业\n露天煤矿-岗位规范-专业技能及专业"
                        )
                )
        ))
    }

    /**
     * 获取责任人
     */
    private fun fetchResponsbleGuys() {
        val params = mutableMapOf(
                Pair(Const.qr_taskid, "$id")
        )
        activity?.apply {
            Http.request()
                    .rxPost("${Const.base_url}${Const.act_task_check_cooper_list}", params)
                    .wrapIO()
                    .subscribe(
                            {},
                            {
                                it.printStackTrace()
                                hideLoader()
                            },
                            {
                                hideLoader()
                            },
                            {
                                dispose = it
                                loading()
                            }
                    )
        }
    }
}

class ChargePeopleAdapter(layoutRes: Int) : BaseQuickAdapter<ChargePeopleModel, BaseViewHolder>(layoutRes) {
    @SuppressLint("SetTextI18n")
    override fun convert(helper: BaseViewHolder?, item: ChargePeopleModel?) {
        if (item == null) return
        helper?.apply {
            getView<ImageView>(R.id.iv_avatar).server(item.avatar)
            setText(R.id.tv_user_name, item.name)
            val task_size = item.tasks.size
            val tvTaskCount = getView<TextView>(R.id.tv_task_count)
            val llTaskContainer = getView<LinearLayout>(R.id.ll_task_container)
            val llTasks = getView<ItemChargeTaskView>(R.id.ll_tasks)
            if (task_size == 0) {
                tvTaskCount.gone(true)
            } else {
                tvTaskCount.gone(false)
                tvTaskCount.text = "${task_size}项任务"
                llTasks.bind(item.tasks)
            }
            getView<ImageView>(R.id.iv_arrow).setOnClickListener {
                if (task_size == 0) return@setOnClickListener
                val visible = llTaskContainer.isVisible()
                llTaskContainer.gone(visible)
            }
        }
    }

}

